<?php

namespace App\Models\Biz;

//use Illuminate\Database\Eloquent\Model;
use App\Models\ExModel;
use Laracasts\Presenter\PresentableTrait;

class Shop extends ExModel  
{
	use PresentableTrait; 

	protected $table = 'biz_shops';

    protected $fillable = ['name','branch_id','staff_id','type','trade','member_id','category_id','progress','address','opening','content'];

	protected $guarded = array();

    protected $with = ['products','account','member','staff','branch','category','category.parent'];
    
	protected $presenter = 'App\Http\Presenters\Biz\ShopPresenter';
    
	public function products()
	{
		return $this->hasMany('\App\Models\Sku\Product');
	}

	public function items()
    {
        return $this->hasManyThrough('\App\Models\Sku\Item', '\App\Models\Sku\Product');
    }


    public function member()
    {
        return $this->belongsTo('\App\Models\Mbr\Member');
    }

    public function staff()
    {
    	return $this->belongsTo('\App\Models\Acl\Staff');
    }


    public function branch()
    {
    	return $this->belongsTo('\App\Models\Mgt\Branch');
    }


    public function account()
    {
    	return $this->hasOne('\App\Models\Biz\Account');
    }

    public function category()
    {
        return $this->belongsTo('\App\Models\Mgt\Category');
    }

    /**
     * 根据登录员工所属分支指定地域归属门店
     * @param  [type] $query [description]
     * @param  [type] $staff [description]
     * @return [type]        [description]
     */
    public function scopeCurrent($query, $staff)
    {
    	$supervisor = \Config::get('auth.supervisor');

    	if (!in_array($staff->id, $supervisor)){
    		return $query->where('branch_id', $staff->branch_id);
    	}
    }

}